﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class ProductView : AuthBasePage
    {


        public string sequenceName
        {
            get { return "MD_Product"; }
        }


        private string m_strTbl = "MD_Product";
        private string m_strMainKey = "ProductID";

        #region   read data
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void InitEditData()
        {
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            GetProductById(strCode);
            InitDtDUpFile();
            InitDtTechFile();
        }

        private void GetProductById(string strCode)
        {
            if (!string.IsNullOrEmpty(strCode))
            {
                MdProductEntity product = MdProduct.GetMdProductByID(strCode);
                if (product != null)
                {
                    this.lblProductID.Text = product.ProductID.Trim();
                    this.lblProductName.Text = product.ProductName.Trim();
                    this.lblUnit.Text = product.Unit.Trim();
                    this.lblSpecifications.Text = product.Specifications.Trim();
                    this.lblSafeStock.Text = product.SafeStock.ToString();
                    this.ddlProductType.SelectedValue = product.ProductTypeID.Trim();
                    this.lblOutSide.Text = product.OutSide.ToString();
                    this.lblInSide.Text = product.InSide.ToString();

                }



            }
        }

        private void BindMapFileData()
        {
            try
            {
                foreach (DataRow dr in DtDUpFile.Rows)
                {
                    dr["FormID"] = lblProductID.Text.Trim();
                }

                grdResultMap.DataSource = DtDUpFile;
                grdResultMap.DataBind();
                UIHelper.RunScript(this.Page, "iframeAutoFit(); ");
            }
            catch { }
        }




        private void BindTechFileData()
        {
            foreach (DataRow dr in DtTechFile.Rows)
            {
                dr["FormID"] = lblProductID.Text.Trim();
            }
            grdResultTech.DataSource = DtTechFile;
            grdResultTech.DataBind();

            UIHelper.RunScript(this.Page, "iframeAutoFit(); ");

        }

      
        //read map file
        private void InitDtDUpFile()
        {

            string ProductID = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(ProductID))
            {

                DataTable dTable = DtDUpFile.Copy();
                InitAttachmentFileDt(ProductID, dTable, "ProductMap");
                DtDUpFile = dTable.Copy();

                BindMapFileData();
            }
        }

        //read tech file
        private void InitDtTechFile()
        {

            string ProductID = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(ProductID))
            {

                DataTable dTable = DtTechFile.Copy();
                InitAttachmentFileDt(ProductID, dTable, "ProductTech");
                DtTechFile = dTable.Copy();

                BindTechFileData();
            }
        }

        private DataTable InitAttachmentFileDt(string ProductID, DataTable dTable, string strFormType)
        {
            DataTable dt = MdAttachment.GetMdAttachment(ProductID, strFormType);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow d = dTable.NewRow();
                    d["AttachmentID"] = CommonMethod.GetString(dr["AttachmentID"]);
                    d["FileType"] = CommonMethod.GetString(dr["FileType"]);
                    d["FileSize"] = CommonMethod.GetString(dr["FileSize"]);
                    d["UpDateTime"] = CommonMethod.GetString(dr["UpDateTime"]);
                    d["FileURL"] = CommonMethod.GetString(dr["FileURL"]);
                    d["FileName"] = CommonMethod.GetString(dr["FileName"]);
                    d["FormType"] = CommonMethod.GetString(dr["FormType"]);
                    d["FormID"] = CommonMethod.GetString(dr["FormID"]);
                    d["Status"] = CommonMethod.GetString(dr["Status"]);
                    d["AttachmentID"] = CommonMethod.GetString(dr["AttachmentID"]);
                    string path = "/UpFile/";
                    path += strFormType + "/" + d["FileURL"];
                    path = Request.Url.Scheme + "://" + Request.Url.Authority + path;
                    d["OpenURL"] = path;
                    dTable.Rows.Add(d);
                }
            }
            return dt;
        }

        #endregion





        #region Attachment File

        /// <summary>
        /// 图纸文件的datatable     与upProductFile 统一
        /// </summary>
        protected DataTable DtDUpFile
        {
            get
            {
                if (Session[SessionMapName] != null)
                {
                    return (DataTable)Session[SessionMapName];
                }
                else
                {
                    return CreateUpFileDT();
                }
            }
            set
            {
                Session[SessionMapName] = value;
            }
        }

        /// <summary>
        /// 技术文件的datatable
        /// </summary>
        public DataTable DtTechFile
        {
            set { Session[SessionTechName] = value; }
            get
            {
                if (Session[SessionTechName] == null)
                {
                    return CreateUpFileDT();
                }
                else
                {
                    return (DataTable)Session[SessionTechName];
                }
            }
        }

        private DataTable CreateUpFileDT()
        {


            return MdProduct.CreateUpFileDT();

        }
        #endregion
        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }


        protected void grdResultMap_RowCommand(object sender, GridViewCommandEventArgs e)
        {
          
        }


        //Tech file Delete
        protected void grdResultTech_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
        }



        static string SessionMapName = "";
        static string SessionTechName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {



                SessionMapName = MdProduct.QuerySessionFileName("5");
                SessionTechName = MdProduct.QuerySessionFileName("6");

                Session[SessionMapName] = null;

                Session[SessionTechName] = null;

                InitEditData();
                BindDDLProductType();

               
            }
            Master.ValidationGroup = "gSubmit";
            UIHelper.RunScript(this.UpdatePanel1, "Refreashtab();iframeAutoFit();");
        }

     
        private void BindDDLProductType()
        {
            DataTable dt = MdProducttype.GetMdProducttypeAll().Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                this.ddlProductType.DataSource = dt;
                this.ddlProductType.DataTextField = "ProductTypeName";
                this.ddlProductType.DataValueField = "ProductTypeID";
                this.ddlProductType.DataBind();
            }

            UIHelper.InsertFirstItem(ddlProductType);
        }

    }
}